package com.qf.automation.file.controller;

import com.qf.automation.file.dto.ImgDTO;
import com.qf.automation.file.dto.PathDTO;
import com.qf.automation.file.model.GifWork;
import com.qf.automation.file.service.FileService;
import com.qf.automation.file.service.GifService;
import com.qf.automation.file.utils.Category;
import com.qf.automation.file.utils.QRCodeUtil;
import com.qf.automation.file.utils.UploadUtil;
import com.qf.base.file.QRCodeFTO;
import com.qf.base.result.R;
import com.qf.base.utils.AsyncBatchCallUtil;
import com.qf.base.utils.FileBase64Util;
import com.qf.base.utils.SyncBatchCallUtil;
import javafx.concurrent.Task;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileService fileService;
    @Resource
    private GifService gifService;
    /**
     *  相片上传
     */
    @PostMapping("/imgs")//上传文件的接口MultipartFile
    public R uploadImage(MultipartFile file, HttpServletRequest request){
        try {
            UploadUtil uploadUtil=new UploadUtil();
            String s = uploadUtil.fileUpload(file,request);
            System.out.println(s);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.toString());
        }
//        return totalBerthStandingBookService.uploadImage(imgDTO);
    }
    @PostMapping("/img")//上传文件的接口MultipartFile
    public R uploadImage(@RequestBody ImgDTO imgDTO){
        String base64= imgDTO.getBase64();
        BufferedImage bufferedImage = FileBase64Util.base64Image(base64);
        String fileName= UUID.randomUUID().toString().replaceAll("-", "");
        String filePath="E:\\TEST\\"+fileName+".png";
        File file=new File(filePath);
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            ImageIO.write(bufferedImage,"png", file);
        } catch (IOException e) {
//            e.printStackTrace();
            return  R.ok(500,"上传失败");
        }
        String url=fileService.saveFile(file,fileName+".png");
        return R.ok(200,"上传成功").setResult(url);
    }



    @PostMapping("/mp4")
    public R upLoadMP4(MultipartFile file){

        return  null;
    }

    public static final  String FILE_PATH="E:\\TEST";
    @PostMapping("/mp4newGif")
    public R uploadMP4newGif(MultipartFile file) throws IOException {
//       生成文件名
        String fileName=UUID.randomUUID().toString().replaceAll("-","")+".mp4";
//         实例保存目标文件
        File targeFile=new File(FILE_PATH+File.separator+fileName);
//        把文件保存起来
        file.transferTo(targeFile);
        // 把文件上传到阿里服务器
        String url = fileService.saveFile(targeFile,fileName);
//        把url放到数据库里,然后设置gif生成的状态为0
        int i = gifService.saveFileUrl(url);
        //异步方式,前台返回为已经完成,然后用另一个线程去生成动图

        AsyncBatchCallUtil.execute(new Runnable() {
            @Override
            public void run() {
                gifService.craeteGif(i,targeFile,fileName,url);
            }
        });

//        返回一个视频的唯一标识 uuid
        return R.ok(200,"gif正在生成,请稍等").setResult(url);
    }
    @PostMapping("/search")
    public R searchGif(@RequestBody PathDTO pathDTO){
        String url=pathDTO.getUrl();
        GifWork gifWork=gifService.selectByUrl(url);
        return R.ok(200,"gif生成成功").setResult(gifWork);
    }
    @PostMapping("/qrcode")
    public R qrcode(@RequestBody QRCodeFTO qrCodeFTO){
        BufferedImage bufferedImage=null;
        String content=qrCodeFTO.getContent();
        if (StringUtils.isEmpty(content)){
            return R.error(501,"请设置二维码的内容");
        }
        int onColor = qrCodeFTO.getOnColor();
        if (StringUtils.isEmpty(onColor)){
            bufferedImage= QRCodeUtil.qrEncode(content,onColor);
        }else {
            int offColor = qrCodeFTO.getOffColor();
            if (StringUtils.isEmpty(offColor)){

                bufferedImage=QRCodeUtil.qrEncode(content,onColor,offColor);
            }else {
                Integer w=qrCodeFTO.getWidth();
                Integer h=qrCodeFTO.getHeight();
                if (w==null||h==null){
                    bufferedImage=QRCodeUtil.qrEncode(content);


                }
            }
        }
        String result = FileBase64Util.image2Base64(bufferedImage);
        return R.ok(200,"二维码生成成功").setResult(result);
    }

    }
